from flask_wtf import FlaskForm
from wtforms import StringField, PasswordField, BooleanField, SubmitField
from wtforms.validators import DataRequired, ValidationError, Email, EqualTo,Length
from app.models import User


class LoginForm(FlaskForm):
    username = StringField(label='用户名', 
                           validators=[
                               DataRequired(message='请输入用户名'),
                               Length(3,10,message="长度3-10")])
    password = PasswordField(
        label = '密码', 
        validators=[
            DataRequired(message="输入密码"),
            Length(3,10,message="长度3-10")
            ])
    repassword = PasswordField(
        label = '确认密码', 
        validators=[
            DataRequired(message="输入密码"),
            Length(3,10,message="长度3-10")
            ])    
    remember_me = BooleanField(label='记住我')
    submit = SubmitField(label='登录')

class RegistrationForm(FlaskForm):
    username = StringField('用户名', validators=[DataRequired()])    
    password = PasswordField('密码', validators=[DataRequired()])
    repassword = PasswordField('确认密码', validators=[DataRequired(), EqualTo('password')])
    phone = StringField('手机号', validators=[DataRequired()])

    submit = SubmitField('注册')

    def validate_username(self, username):
        user = User.query.filter_by(username=username.data).first()
        if user is not None:
            raise ValidationError('Please use a different username.')

    def validate_phone(self, phone):
        user = User.query.filter_by(phone=phone.data).first()
        if user is not None:
            raise ValidationError('Please use a different phone.')